package com.cy.store.repository;

import com.cy.store.entity._Address;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import javax.print.attribute.standard.PageRanges;
import java.util.Date;
import java.util.List;

public interface AddressRepository extends JpaRepository<_Address, Integer> {
    //按uid统计地址
    Integer countByUid(int i);

    //显示当前登录用户的收货地址
    List<_Address> findAllByUidOrderByIsDefaultDescCreatedTimeDesc(Integer uid);


    //地址设为非默认存在
    @Modifying
    @Transactional
    @Query("update _Address set isDefault=0 where uid=?1")
    Integer updateNonDefaultByUid(Integer uid);

    //地址设为默认存在
    @Modifying
    @Transactional
    @Query("update _Address set isDefault=1, modifiedUser=?2, modifiedTime=?3 where aid=?1")
    Integer updateDefaultByAid(Integer aid, String modifiedUser, Date modifiedTime);

    //检索最近修改的地址
    Page<_Address> findAllByUidOrderByModifiedTimeDesc(Integer uid, Pageable pageable);


}
